package com.physics.mps.module.comsol.builder.mesh;

import com.physics.mps.module.comsol.builder.Builder;
import com.physics.mps.module.comsol.vo.MeshVo;
import com.physics.mps.module.comsol.vo.SizeVo;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author mcsoft
 * @date 2023/10/16 11:59
 * @description comsol java 网格脚本构建
 */
public class MeshBuilder {


    private final Builder builder;

    public MeshBuilder(Builder builder) {
        this.builder = builder;
    }


    public void build(String geomTag, List<MeshVo> vos) {
        if (CollectionUtils.isEmpty(vos)) {
            return;
        }
        for (MeshVo vo : vos) {
            build(geomTag, vo);
        }
    }
    public void build(String geomTag, MeshVo vo) {
        builder.echo("Mesh");

        String meshTag = builder.uniqueTag("mesh");
        builder.change(1, 2);
        builder.append("comp.mesh().create(");
        builder.value(meshTag);
        builder.append(");");

        String ftetTag = builder.uniqueTag("ftet");

        // 自由四面体网格
        builder.change(1, 2);
        builder.append("MeshSequence meshSequence = comp.mesh(");
        builder.value(meshTag);
        builder.append(");");

        builder.change(1, 2);
        builder.append("meshSequence.create(");
        builder.value(ftetTag);
        builder.append(", \"FreeTet\");");

        builder.change(1, 2);
        builder.append("meshSequence.run();");


        builder.change(1, 2);
        builder.append("meshSequence.automatic(false);");
        builder.change(1, 2);
        builder.append("meshSequence.feature(\"size\").set(\"hmax\", ");
        builder.append(vo.getMax());
        builder.append(");");
        builder.change(1, 2);
        builder.append("meshSequence.feature(\"size\").set(\"hmin\", ");
        builder.append(vo.getMin());
        builder.append(");");

        createSize(geomTag, ftetTag, vo.getSizes());
    }


    /**
     * 创建网格大小
     * @param geomTag
     * @param ftetTag
     * @param sizes
     */
    private void createSize(String geomTag, String ftetTag, List<SizeVo> sizes) {
        if (CollectionUtils.isEmpty(sizes)) {
            return;
        }

        builder.change(1, 2);
        builder.append("MeshFeature meshFeature = meshSequence.feature(");
        builder.value(ftetTag);
        builder.append(");");


        sizes.forEach(vo -> {

            String sizeTag = builder.uniqueTag("size");
            String meshFeature = builder.uniqueTag("meshFeature");

            builder.change(2, 2);
            builder.append("MeshFeature ");
            builder.append(meshFeature);
            builder.append(" =  meshFeature.create(");
            builder.value(sizeTag);
            builder.append(", \"Size\");");

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".set(\"custom\", true);");

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".set(\"hmaxactive\", true);");

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".set(\"hminactive\", true);");

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".set(\"hmax\", ");
            builder.append(vo.getMax());
            builder.append(");");

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".set(\"hmin\", ");
            builder.append(vo.getMin());
            builder.append(");");

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".selection().geom(");
            builder.value(geomTag);
            builder.append(", ");
            builder.append(vo.getEntityDim());
            builder.append(");");

            builder.change(1, 2);
            String ballSelect = builder.createBallSelect(vo.getEntityDim(), vo.getCoordinate());

            builder.change(1, 2);
            builder.append(meshFeature);
            builder.append(".selection().named(\"");
            builder.append(geomTag);
            builder.append("_");
            builder.append(ballSelect);
            builder.append("\");");

        });

    }



}
